using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;

namespace Nairo
{
	public class RenderUnit
	{
		protected List<RenderItem> myRenderItems;
		protected int maxWidth;
		protected int myWidth;
		protected int myHeight;
		protected int myTop;
		protected int left;
		protected int boxHeight;
		
		public int BoxHeight {
			get { return boxHeight; }
			set { boxHeight = value; }
		}
		
		public int Left {
			get { return left; }
			set { left = value; }
		}
		
		public int MaxWidth
		{
			get { return maxWidth; }
			set { maxWidth = value; }
		}
		
		public int CurrentWidth
		{
			get { return myWidth; }
			set { myWidth = value; }
		}
		
		public int Height
		{
			get { return myHeight; }
			set { myHeight = value; }
		}
		
		public int Top
		{
			get { return myTop; }
			set { myTop = value; }
		}
		
		public RenderItem FirstItem
		{
			get {
				if (myRenderItems.Count == 0)
					return null;
				
				return myRenderItems[0];
			}
		}
		
		public RenderUnit(int targetwidth)
		{
			maxWidth = targetwidth;
			myRenderItems = new List<RenderItem>();
			myHeight = 0;
			myWidth = 0;
			this.left = 8;
		}
		
		public int RemainingWidth
		{
			get {
				if (myWidth < maxWidth)
					return maxWidth - myWidth;
				
				return 0;
			}
		}
		
		public int GetItemCount()
		{
			return myRenderItems.Count;
		}
		
		public void Add(RenderItem item)
		{
			myHeight = Math.Max(myHeight, item.Height);
			myWidth += item.Width;
			myRenderItems.Add(item);
		}
		
		public void Add(IEnumerable<RenderItem> items)
		{
			foreach (RenderItem item in items) {
				myHeight = Math.Max(myHeight, item.Height);
				myWidth += item.Width;
				myRenderItems.Add(item);
			}
		}
		
		/// <summary>
		/// Called by HTMLRenderer after bands are filled
		/// with RenderItems so alignment can be calculated
		/// <summary>
		public void ComputeItemCoords()
		{
			int left = 1;
			int marginTop = 0;
			int marginBottom = 0;
			
			if (myRenderItems.Count == 0) return;
			RenderItem hri = myRenderItems[0] as RenderItem;
			
			foreach (RenderItem item in myRenderItems)
			{
				item.RenderUnit = this;
				item.Left = this.Left + left;
				item.Top = this.Top;
				left += item.Width;
				
				marginBottom = Math.Max(item.Element.MarginBottom, marginBottom);
				marginTop = Math.Max(item.Element.MarginTop, marginTop);
			}
			
			this.boxHeight = this.Height;
		}
		
		public void Paint(Graphics g)
		{
//			Pen p=new Pen(Color.Red);
//			g.DrawRectangle(p, 0, this.Top, this.CurrentWidth, this.Height);
			foreach (RenderItem item in myRenderItems)
			{
				item.Paint(g);
			}
		}
		
		/// <summary>
		/// Returns the item whose
		/// bounding rectangle contains the specified point.
		/// Used for hyperlink hit-testing.
		/// </summary>
		public RenderItem LocateItem(int x, int y)
		{
			foreach (RenderItem item in this.myRenderItems)
			{
				if ((x >= item.Left) && (x <= item.Left + item.Width))
				{
					if ((y >= item.Top) && (y <= item.Top + item.Height))
					{
						return item;
					}
				}
				
			}
			return null;
		}
	}
}
